import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Not, In, Like } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { ResultData } from 'src/common/utils/result';
import { CreateTemplateDto, UpdateTemplateDto, QueryTemplateDto } from './dto/template.dto';
import { TemplateEntity } from './entities/template.entity';
import { isEmpty } from 'src/common/utils';

@Injectable()
export class TemplateService {
  constructor(
    @InjectRepository(TemplateEntity)
    private readonly templateEntityRep: Repository<TemplateEntity>,
  ) {}

  async create(createTemplateDto: CreateTemplateDto) {
    const res = await this.templateEntityRep.save(createTemplateDto);
    return ResultData.ok(res);
  }

  async findAll(query: QueryTemplateDto) {
    const entity = this.templateEntityRep.createQueryBuilder('entity');
    entity.where({ delFlag: '0' });
    if (!isEmpty(query.id)) {
      entity.andWhere('entity.id = :id', { id: query.id });
    }
    if (!isEmpty(query.name)) {
      entity.andWhere('entity.name LIKE :name', { name: `%${query.name}%` });
    }
    if (!isEmpty(query.templateCode)) {
      entity.andWhere('entity.templateCode = :templateCode', { templateCode: query.templateCode });
    }
    if (!isEmpty(query.signName)) {
      entity.andWhere('entity.signName LIKE :signName', { signName: `%${query.signName}%` });
    }
    if (!isEmpty(query.delFlag)) {
      entity.andWhere('entity.delFlag = :delFlag', { delFlag: query.delFlag });
    }
    if (!isEmpty(query.createTime)) {
      entity.andWhere('entity.createTime BETWEEN :start AND :end', { start: query.createTime[0], end: query.createTime[1] });
    }
    if (!isEmpty(query.updateTime)) {
      entity.andWhere('entity.updateTime BETWEEN :start AND :end', { start: query.updateTime[0], end: query.updateTime[1] });
    }
    entity.select(['entity.id', 'entity.name', 'entity.templateCode', 'entity.signName']);

    if (query.orderByColumn && query.isAsc) {
      const key = query.isAsc === 'ascending' ? 'ASC' : 'DESC';
      entity.orderBy(`entity.${query.orderByColumn}`, key);
    }

    if (query.pageNum && query.pageSize) {
      entity.skip(query.pageSize * (query.pageNum - 1)).take(query.pageSize);
    }

    const [list, total] = await entity.getManyAndCount();

    return ResultData.ok({
      list,
      total,
    });
  }

  async findOne(id: number) {
    const res = await this.templateEntityRep.findOne({
      where: {
        delFlag: '0',
        id: id,
      },
    });
    return ResultData.ok(res);
  }

  async findUser(id: number) {
    return await this.templateEntityRep.findOne({
      where: {
        delFlag: '0',
        id: id,
      },
    });
  }

  async update(updateTemplateDto: UpdateTemplateDto) {
    const res = await this.templateEntityRep.update({ id: updateTemplateDto.id }, updateTemplateDto);
    return ResultData.ok({ value: res.affected >= 1 });
  }

  async remove(ids: number[]) {
    const res = await this.templateEntityRep.update(
      { id: In(ids) },
      {
        delFlag: '1',
      },
    );
    return ResultData.ok({ value: res.affected >= 1 });
  }
}
